//
//  ChannerViewController.m
//  ProjectOfBStage
//
//  Created by cuixin on 15/11/11.
//  Copyright © 2015年 cuixin. All rights reserved.
//

#import "ChannerViewController.h"
#import "CustomerNetworking.h"
#import "CXChannelCollectionViewCell.h"
#import "ChannerModel.h"
#import <MJRefresh.h>
#import "CXHeaderView.h"
#import "CXChannelInterActionController.h"
#import "CXChannelCollectionResuableView.h"
#import "CXNewChannelCollectionCell.h"

#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
@interface ChannerViewController ()<UIScrollViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, retain)UIScrollView *scrollView;

@property(nonatomic, retain)UICollectionView *collectionView;

@property(nonatomic, retain)UICollectionView *secondCollectionView;

@property(nonatomic, retain)NSMutableArray *firstArray;

@property(nonatomic, retain)NSMutableArray *secondArray;

@property(nonatomic, assign)NSInteger page;

@property(nonatomic, assign)BOOL loading;

@property(nonatomic, retain)CXHeaderView *headerView;
@end

@implementation ChannerViewController

-(void)dealloc{

    [_scrollView release];
    [_collectionView release];
    [_firstArray release];
    [_headerView release];
    [_secondCollectionView release];
    [_secondArray release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
//    [self loadData];
    self.firstArray = [NSMutableArray array];
    self.secondArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createSearch];
    [self createNavigationTitileButton];
    [self createScrollView];
    
    CXChannelInterActionController *interActionController = [[CXChannelInterActionController alloc] init];
    [self addChildViewController:interActionController];
    interActionController.view.frame = CGRectMake(kWidth, 0, kWidth, kHeight);
    [self.scrollView addSubview:interActionController.view];
    
    
    [self createCollectionView];

    [self createHeaderView];
    
    [self header];
    [self footer];

}

-(void)header{
    self.collectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        self.loading =NO;
        [self loadData:self.page andLoading:NO];
    }];
    [self.collectionView.header beginRefreshing];
    
    
}

-(void)footer{
    self.collectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page ++;
        self.loading = YES;
        [self loadData:self.page andLoading:YES];
    }];
    
    
}

-(void)secondHeader{
    self.secondCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        self.loading =NO;
        [self loadNewData:self.page andLoading:NO];
        
    }];
    [self.secondCollectionView.header beginRefreshing];
}
-(void)secondFooter{
    self.secondCollectionView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        
        self.page++;
        self.loading = YES;
        [self loadNewData:self.page andLoading:YES];
    }];
}
#pragma mark 解析最热数据
-(void)loadData:(NSInteger)page andLoading:(BOOL)loading{
    [CustomerNetworking customerWithURL:[NSString stringWithFormat:@"http://echosystem.kibey.com/channel/get?with_sound=0&order=hot&page=%ld", page] andParamer:nil andBlock:^(id response) {
        if (loading == NO) {
            [self.firstArray removeAllObjects];
        }
        
        for (NSDictionary *dic in [[response objectForKey:@"result"] objectForKey:@"data"]) {
            ChannerModel *model = [ChannerModel modelWithDictionary:dic];
            [self.firstArray addObject:model];
        }
        [self.collectionView reloadData];
        [self.collectionView.header endRefreshing];
        [self.collectionView.footer endRefreshing];
        self.collectionView.header.hidden = YES;
    }];
}
#pragma mark 解析最热数据
-(void)loadNewData:(NSInteger)page andLoading:(BOOL)loading{
    [CustomerNetworking customerWithURL:[NSString stringWithFormat:@"http://echosystem.kibey.com/channel/get?with_sound=0&order=new&page=%ld", page] andParamer:nil andBlock:^(id response) {
        if (loading == NO) {
            [self.secondArray removeAllObjects];
        }
        
        for (NSDictionary *dic in [[response objectForKey:@"result"] objectForKey:@"data"]) {
            ChannerModel *model = [ChannerModel modelWithDictionary:dic];
            [self.secondArray addObject:model];
        }
        
        [self.secondCollectionView reloadData];
        [self.secondCollectionView.header endRefreshing];
        [self.secondCollectionView.footer endRefreshing];
        self.secondCollectionView.header.hidden = YES;
    }];
}

-(void)createHeaderView
{
    self.headerView = [[CXHeaderView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 30)];
    self.headerView.backgroundColor = [UIColor whiteColor];
    [self.headerView.hotButton addTarget:self action:@selector(hotButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView.newwButton addTarget:self action:@selector(newwButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.headerView];
    
}

-(void)newwButtonAction:(UIButton *)button{
    self.collectionView.hidden = YES;
    [self.headerView.hotButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.headerView.newwButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];

    [self createSecondCollectionView];
    [self secondHeader];
    [self secondFooter];

    
}
-(void)hotButtonAction:(UIButton *)button{
    self.collectionView.hidden = NO;
    self.secondCollectionView.hidden = YES;
    [self.headerView.newwButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.headerView.hotButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    
}
#pragma mark 创建collectionView
-(void)createCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 60) collectionViewLayout:flowLayout];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView setContentInset:UIEdgeInsetsMake(40, 0, 0, 0)];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //注册cell
    [self.collectionView registerClass:[CXChannelCollectionViewCell class] forCellWithReuseIdentifier:@"CXChannelCollectionViewCellID"];
    [self.scrollView addSubview:self.collectionView];
    [_collectionView release];
    [flowLayout release];
}

-(void)createSecondCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.secondCollectionView = [[UICollectionView alloc] initWithFrame:self.collectionView.frame collectionViewLayout:flowLayout];
    self.secondCollectionView.backgroundColor = [UIColor whiteColor];
    [self.secondCollectionView setContentInset:UIEdgeInsetsMake(40, 0, 0, 0)];
    self.secondCollectionView.delegate = self;
    self.secondCollectionView.dataSource = self;
    //注册cell
    [self.secondCollectionView registerClass:[CXNewChannelCollectionCell class] forCellWithReuseIdentifier:@"CXNewChannelCollectionCellID"];
    [self.scrollView addSubview:self.secondCollectionView];
    [_secondCollectionView release];
    [flowLayout release];
}

#pragma mark 创建scrollView
-(void)createScrollView{
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 60)];
    self.scrollView.scrollEnabled = NO;
    self.scrollView.delegate = self;
    self.scrollView.backgroundColor = [UIColor whiteColor];
    self.scrollView.bounces = NO;
    self.scrollView.pagingEnabled = NO;
    self.scrollView.contentSize = CGSizeMake(self.view.frame.size.width * 2, 0);
    [self.view addSubview:self.scrollView];
}
#pragma mark 创建左边search小按钮
-(void)createSearch{
    UIButton *searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchButton setFrame:CGRectMake(10, 10, 25, 25)];
    [searchButton setBackgroundImage:[UIImage imageNamed:@"iconfont-sousuo"] forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(searchButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:searchButton];
}


#pragma mark 创建中间的titleButton
-(void)createNavigationTitileButton{
    
    UIButton *firstButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [firstButton setFrame:CGRectMake(140, 10, 30, 20)];
    [firstButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [firstButton setTitle:@"探索" forState:UIControlStateNormal];
    [firstButton addTarget:self action:@selector(firstButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [firstButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:firstButton];
    
    
    UIButton *secondButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [secondButton setFrame:CGRectMake(firstButton.frame.size.width + firstButton.frame.origin.x + 20, firstButton.frame.origin.y, firstButton.frame.size.width, firstButton.frame.size.height)];
    [secondButton.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [secondButton setTitle:@"互动" forState:UIControlStateNormal];
    [secondButton addTarget:self action:@selector(secondButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [secondButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:secondButton];
    
    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [playButton setFrame:CGRectMake(self.view.frame.size.width - 40, secondButton.frame.origin.y, 20, 20)];
    [playButton setBackgroundImage:[UIImage imageNamed:@"iconfont-yuleguangdie.png"] forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:playButton];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (self.firstArray.count != 0) {
        if (collectionView == self.collectionView) {
            return self.firstArray.count;
        }
    }if (self.secondCollectionView != 0) {
        if (collectionView == self.secondCollectionView) {
            return self.secondArray.count;
        }
    }
    return 0;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.collectionView) {
        CXChannelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CXChannelCollectionViewCellID" forIndexPath:indexPath];
        ChannerModel *model = [self.firstArray objectAtIndex:indexPath.row];
        cell.channelModel = model;
        return cell;
    }if (collectionView == self.secondCollectionView) {
        CXNewChannelCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CXNewChannelCollectionCellID" forIndexPath:indexPath];
        ChannerModel *model = [self.secondArray objectAtIndex:indexPath.row];
        cell.channelModel = model;
        return cell;
    }
    
    return nil;
   
}

-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 5;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(184, 100);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(0, 0, 5, 5);
}
#pragma mark  探索点击事件
-(void)firstButtonAction:(UIButton *)button{
    self.scrollView.contentOffset = CGPointMake(0, 0);
    self.headerView.hidden = NO;

}

#pragma mark 互动点击事件
-(void)secondButtonAction:(UIButton *)button{
    self.scrollView.contentOffset = CGPointMake(kWidth, 0);
    self.headerView.hidden = YES;
    
}

#pragma mark 搜索按钮的点击事件
-(void)searchButtonAction:(UIButton *)button{
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
